package liumengbaio.lolzhangyoubao.Activity.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import liumengbaio.lolzhangyoubao.Activity.InFo.MoreTreasureHeadInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * PopupWindow的类
 * Created by lanouhn on 16/9/12.
 */
public class MorePopUtils extends PopupWindow {
    private View view;
    private EditText editText;
    private ImageView down, delete, add;
    private TextView one, two, three, amount;
    private Button go;

    public MorePopUtils(Context context, View.OnClickListener clicks, MoreTreasureHeadInfo.DataBean dataBean) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.more_treasure_popwindow, null);

        add = (ImageView) view.findViewById(R.id.more_treasure_add);
        down = (ImageView) view.findViewById(R.id.more_pop_back);
        delete = (ImageView) view.findViewById(R.id.more_treasure_delete);
        one = (TextView) view.findViewById(R.id.more_pop_one);
        two = (TextView) view.findViewById(R.id.more_pop_two);
        three = (TextView) view.findViewById(R.id.more_pop_three);
        amount = (TextView) view.findViewById(R.id.more_pop_amount);
        editText = (EditText) view.findViewById(R.id.more_treasure_edite);
        go = (Button) view.findViewById(R.id.more_pop_go);

        one.setText("" + dataBean.getRaid_score_options().get(0));
        two.setText("" + dataBean.getRaid_score_options().get(1));
        three.setText("" + dataBean.getRaid_score_options().get(2));
        amount.setText("" + dataBean.getPrice());

        down.setOnClickListener(clicks);
        delete.setOnClickListener(clicks);
        add.setOnClickListener(clicks);
        one.setOnClickListener(clicks);
        two.setOnClickListener(clicks);
        three.setOnClickListener(clicks);
        go.setOnClickListener(clicks);

        //显示弹出框
        this.setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置弹窗可点击
        this.setFocusable(false);
        this.setOutsideTouchable(false);
        //设置弹出窗体的动画效果
        this.setAnimationStyle(R.style.AppTheme);
        ColorDrawable cd = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(cd);

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = view.findViewById(R.id.more_pop_back).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    public void addOne() {
        editText.setText(one.getText().toString());
    }

    public void addTwo() {
        editText.setText(two.getText().toString());
    }

    public void addThree() {
        editText.setText(three.getText().toString());
    }

    public void add() {
        if (!"".equals(editText.getText().toString().trim())) {
            int number = Integer.valueOf(editText.getText().toString().trim());
            number += 1;
            editText.setText("" + number);
        } else {
            editText.setText("" + 1);
        }
    }

    public void delete() {
        if (!"".equals(editText.getText().toString().trim())) {
            int number = Integer.valueOf(editText.getText().toString().trim());
            if (number <= 0) {
                number = 0;
            } else {
                number -= 1;
            }
            editText.setText("" + number);
        } else {
            editText.setText("" + 1);
        }
    }
}
